package com.century.sjt.activity.Pay;

import android.os.Bundle;
import android.os.Handler;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.util.DisplayMetrics;
import android.view.Display;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.century.sjt.R;
import com.century.sjt.activity.BaseActivity;
import com.century.sjt.fragment.PayNoTransactionFragment;
import com.century.sjt.fragment.PayTransactionFragment;
import com.century.sjt.widget.TitleBar.SjtTitleBar;

import java.util.ArrayList;
import java.util.List;

/**
 * 普通用户交易记录
 */
public class TransactionActivity extends BaseActivity {

    private Handler mHandler;

    private ViewPager mViewPager;
    private FragmentPagerAdapter mAdapter;
    private List<Fragment> mDatas;

    private TextView mPayTextView;
    private TextView mNoTextView;
    private LinearLayout mChatLinearLayout;
    //private BadgeView mBadgeView;

    private ImageView mTabline;
    private int mScreen1_2;

    private int mCurrentPageIndex;

    private LinearLayout mTab1, mTab2;

    private SjtTitleBar titleBar;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.sjt_transaction_activity);
        titleBar = (SjtTitleBar) findViewById(R.id.transaction_title);
        //       标题栏
        titleBar.setTitle(getResources().getString(R.string.text_t_pay));
        titleBar.setLeftIconResource(getResources().getString(R.string.icon_return));
        titleBar.setLeftLayoutClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });

        initTabLine();
        initView();
    }

    private void initTabLine() {
        mTabline = (ImageView) findViewById(R.id.id_iv_tabline);
        Display display = getWindow().getWindowManager().getDefaultDisplay();
        DisplayMetrics outMetrics = new DisplayMetrics();
        display.getMetrics(outMetrics);
        mScreen1_2 = outMetrics.widthPixels / 2;
        ViewGroup.LayoutParams lp = mTabline.getLayoutParams();
        lp.width = mScreen1_2;
        mTabline.setLayoutParams(lp);
    }
    @Override
    protected void initView() {
        mViewPager = (ViewPager) findViewById(R.id.viewpager);
        mPayTextView = (TextView) findViewById(R.id.id_t_payt);
        mNoTextView = (TextView) findViewById(R.id.id_t_nopay);

        mTab1 = (LinearLayout) findViewById(R.id.tran_tab1);
        mTab2 = (LinearLayout) findViewById(R.id.tran_tab2);

        mDatas = new ArrayList<Fragment>();

        //Fragement界面
        PayTransactionFragment tab01 = new PayTransactionFragment();
        PayNoTransactionFragment tab02 = new PayNoTransactionFragment();

        mDatas.add(tab01);
        mDatas.add(tab02);


        mAdapter = new FragmentPagerAdapter(getSupportFragmentManager()) {
            @Override
            public int getCount() {
                return mDatas.size();
            }

            @Override
            public Fragment getItem(int arg0) {
                return mDatas.get(arg0);
            }
        };
        mViewPager.setAdapter(mAdapter);

        mTab1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                resetTextView();
//                mPayTextView.setTextColor(getResources().getColor(R.color.textdefault));
                mViewPager.setCurrentItem(0);
            }
        });

        mTab2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                resetTextView();
//                mNoTextView.setTextColor(getResources().getColor(R.color.textdefault));
                mViewPager.setCurrentItem(1);
            }
        });


        intoVP();

    }

    @Override
    protected void initEvent() {

    }

    @Override
    protected void initData() {

    }

    private void intoVP() {
        mViewPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageSelected(int position) {
                resetTextView();
                switch (position) {
                    case 0:
                        mPayTextView.setTextColor(getResources().getColor(R.color.textdefault_title));
                        break;
                    case 1:
                        mNoTextView.setTextColor(getResources().getColor(R.color.textdefault_title));
                        break;
                }

                mCurrentPageIndex = position;

            }

            @Override
            public void onPageScrolled(int position, float positionOffset,
                                       int positionOffsetPx) {
//                Log.e("TAG", position + " , " + positionOffset + " , "
//                        + positionOffsetPx);

                LinearLayout.LayoutParams lp = (LinearLayout.LayoutParams) mTabline
                        .getLayoutParams();

                if (mCurrentPageIndex == 0 && position == 0)// 0->1
                {
                    lp.leftMargin = (int) (positionOffset * mScreen1_2 + mCurrentPageIndex
                            * mScreen1_2);
                } else if (mCurrentPageIndex == 1 && position == 0)// 1->0
                {
                    lp.leftMargin = (int) (mCurrentPageIndex * mScreen1_2 + (positionOffset - 1)
                            * mScreen1_2);
                } else if (mCurrentPageIndex == 1 && position == 1) // 1->2
                {
                    lp.leftMargin = (int) (mCurrentPageIndex * mScreen1_2 + positionOffset
                            * mScreen1_2);
                }
                mTabline.setLayoutParams(lp);

            }

            @Override
            public void onPageScrollStateChanged(int arg0) {

            }
        });
    }

    protected void resetTextView() {
        mPayTextView.setTextColor(getResources().getColor(R.color.textdefault_title));
        mNoTextView.setTextColor(getResources().getColor(R.color.textdefault_title));
    }

    @Override
    protected void onResume() {
        super.onResume();

    }
}
